<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">


<head>

<meta http-equiv="Content-Language" content="en-gb" />

<title>Sycamore</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />

<!-- style sheet links -->
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="menu.css" />

</head>

<body>

<div id="headercontainer">
	<div id="header">
		<h1>Sycamore</h1>
	</div>
</div>

<div id="menucontainer">
	<div id="menu">
		<div id="navcontainer">
			<ul id="navlist">
				<li><a title="Home" href="index.html">Home</a></li>
				<li><a title="Sign Up" href="register_page.html">Sign Up</a></li>
				<li><a title="Download" href="downloads.html">Download</a></li>
			</ul>
		</div>
	</div>
</div>

<div id="content">

	<h2>Sign Up</h2>
<?php
session_start();
   // include function files for this application
   require('fns_db.php');
   require("Page.php");
   // Instantiate the Page class
   $webPage = new Page();    
   //get form data
   $username=$_POST['username'];
   $passwd=$_POST['passwd'];
   $passwd2=$_POST['passwd2'];

   try
   {
      // check forms filled in
      $filled = true;
      foreach ($_POST as $key => $value)
      {
         if (!isset($key) || ($value == '')) 
            $filled = false;
      } 
      if (!$filled)
      {
         throw new Exception('You have not filled the form out correctly 
          - please try again.');    
      }

      // passwords not the same 
      if ($passwd != $passwd2)
      {
         throw new Exception('The passwords you entered do not match 
                - please try again.');
      }

      // check password length is ok
      if (strlen($passwd)<6 )
      {
         throw new Exception('Your password must be >=6 characters.'
                           .'Please try again.');
      }
   
      // attempt to register
      // connect to db
      $conn = db_connect();
    
      // check if username is unique 
      $select ="SELECT * FROM Users WHERE username='$username'";
      $result = mysql_query($select); 
      if (!$result)
         throw new Exception('Could not execute query :'.$select 
		                          . ':' . mysql_error());
      if (mysql_num_rows($result)>0) 
         throw new Exception('That username is taken
                              - go back and choose another one.');

      // if ok, put in db
      $pw = sha1($passwd);
      $insert = "INSERT INTO Users VALUES('$username','$pw')";
      $result = mysql_query($insert);
      if (!$result)
         throw new Exception('problem with '. $insert
		                      . ':' . mysql_error());
      // register session variable 
      $_SESSION['username'] = $username;

      // show home page with user menu
      $webPage->addHeader('Registration successful');
      $webPage->addUserMenu();
      $webPage->addContent('Your registration was successful.');
   }
   catch (Exception $e)
   {
      $webPage->addHeader('Problem with registration details:');
      $webPage->addContent( $e->getMessage()); 
      $webPage->addContent("<br/><a href=register_form.php>Try again</a><br />");	  
   } 

   // Add the footer to the page
   $webPage->addFooter();

   // Display the page
   echo $webPage->getPageContent();
?>


</div>

<div id="footer">
<!--	web design by <a title="derby web design" href="http://www.tristarwebdesign.co.uk">tri-star web design</a> -->
<br></br>
</div>

</body>

</html>
